package com.xhji.example;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(HttpServletRequest request){
        try {
            System.out.println(new Date());
            InetAddress addr = InetAddress.getLocalHost();
            String ip=addr.getHostAddress().toString(); //获取本机ip
            String host=addr.getHostName().toString(); //获取本机计算机名称
            System.out.println(ip + " " + host);
            request.setAttribute("ip",ip);
            request.setAttribute("host",host);
            HttpSession session = request.getSession();
            String random = request.getParameter("random");
            if(StringUtils.hasText(random)){
                session.setAttribute("random",random);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "index";
    }
}
